import re
import requests
import json
import time
import io
from fontTools.ttLib import TTFont
import base64

def getPage(url):
	try:
		i=1
		data = {'ClickID':i}

		headers = {
		'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
		}

		res = requests.get(url,params=data,headers=headers)
		if res.status_code == 200:
			# 处理字体乱码
			base64_str = re.findall('data:application/font-ttf;charset=utf-8;base64,(.*)\'\) format\(\'truetype\'\)}', res.text)
			bin_data = base64.b64decode(base64_str[0])
			fonts = TTFont(io.BytesIO(bin_data))
			bestcmap = fonts.getBestCmap()
			newmap = {}
			for key in bestcmap.keys():
				value = int(re.findall(r'(\d+)',bestcmap[key])[0])-1
				key = hex(key)
				newmap[key] = value
			res_ = res.text
			for key,value in newmap.items():
				key_ = key.replace('0x', '&#x') + ';'
				if key_ in res_:
					res_ = res_.replace(key_, str(value))
			return res_
		else:
			return None
		
	except Exception as e:
		print(e)
		return None

def parsePage(html):
	# 使用正则表达式解析
	pat = ('<li  class="house-cell".*?<div class="img-list">.*?lazy_src="(.*?)"\\n.*?'
			'<div class="des">.*?<a href.*?>(.*?)</a>.*?'
			'<p class="room">(.*?)&nbsp;&nbsp;&nbsp;&nbsp;(.*?)</p>.*?'
			'<b class="strongbox">(.*?)</b>.*?</li>')
	# re.S 使得 . 还能匹配换行符
	items = re.findall(pat,html,re.S)
	for item in items:
		yield({
			
			'title':item[1][3:].strip(),
			'img':item[0],
			'size':item[2]+item[3],
			'price':item[4]+"元/月"
			
			})


def writeFile(content):
	# with 打开不用关闭
	with open('./py_58_info.txt','a',encoding='utf-8') as f:
		f.write(json.dumps(content,ensure_ascii=False)+'\n')

def main(page):
	url = 'https://bj.58.com/dashanzi/chuzu/pn'+str(page)+'/?'
	# url = 'http://bj.58.com/dashanzi/chuzu/pn1/?'

	html = getPage(url)

	content = parsePage(html)
	for i in content:
		writeFile(i)
	

if __name__=='__main__':
	for page in range(1,26):
		main(page)
		time.sleep(1)
